﻿Public Class Product
    Public l As New Label
    Public cate As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If txtcode.Text.Trim = "" Then
            MessageBox.Show("Product Code can't be empty")
            txtcode.Focus()
            Exit Sub
        ElseIf txtname.Text.Trim = "" Then
            MessageBox.Show("Product Name Can't be Empty")
            txtname.Focus()
            Exit Sub
        ElseIf txtprice.Text.Trim = "" Then
            MessageBox.Show("Price can't be empty")
            txtprice.Focus()
            Exit Sub
        ElseIf txtminprice.Text.Trim = "" Then
            MessageBox.Show("Minimum Price can't be empty")
            txtminprice.Focus()
            Exit Sub
        End If

        If Validater.IsDecimal(txtprice.Text) = False Then
            MessageBox.Show("Price could be Decimal")
            txtprice.Focus()
            txtprice.SelectAll()
            Exit Sub
        ElseIf Validater.IsDecimal(txtminprice.Text) = False Then
            MessageBox.Show("Minnimum Price could be Decimal")
            txtminprice.Focus()
            txtminprice.SelectAll()
            Exit Sub
        End If

        cmd.Parameters("@code") = txtcode.Text
        cmd.Parameters("@name") = txtname.Text
        cmd.Parameters("@cateid") = cbocategory.SelectedValue
        cmd.Parameters("@price") = txtprice.Text
        cmd.Parameters("@minprice") = txtminprice.Text
        If Me.l.Text = "" Then
            cmd.ExecuteNonQuery("INSERT INTO Product VALUES(@code,@name,@cateid,@price,@minprice,1);")

        Else
            cmd.Parameters("@proId") = l.Text
            cmd.ExecuteNonQuery("UPDATE Product SET ProductName=@name,CategoryId=@cateid,Price=@price,MinimumPrice=@minprice WHERE ProductId=@proId")
        End If
        Me.DialogResult = Windows.Forms.DialogResult.OK

    End Sub

    Private Sub Product_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadtype()
        ' loadcate()
        loadcaaa()
        'cbocategory.Enabled = False
        If cate <> 0 Then
            cbocategory.SelectedValue = cate
            cmd.Parameters("@id") = cate
            Dim a As Object = cmd.ExecuteScalar("SELECT TypeId FROM Category WHERE CategoryId=@id").ToString
            cbotype.SelectedValue = a
            ' cbocategory.Enabled = True
        End If
    End Sub
    Private Sub loadtype()
        cbotype.ValueMember = ("TypeId")
        cbotype.DisplayMember = ("TypeName")
        cbotype.DataSource = cmd.ExecuteDataTable("SELECT TypeId,TypeName FROM [Type] WHERE Active=1")
    End Sub
    Private Sub loadcaaa()
        cbocategory.ValueMember = ("CategoryId")
        cbocategory.DisplayMember = ("CategoryName")
        cmd.Parameters("@types") = cbotype.SelectedValue
        cbocategory.DataSource = cmd.ExecuteDataTable("SELECT CategoryId,CategoryName FROM Category WHERE Active=1 and TypeId=@types")
    End Sub

    Private Sub loadcate()
        cbocategory.ValueMember = ("CategoryId")
        cbocategory.DisplayMember = ("CategoryName")
        cmd.Parameters("@types") = cbotype.SelectedValue
        cbocategory.DataSource = cmd.ExecuteDataTable("SELECT CategoryId,CategoryName FROM Category WHERE Active=1")
    End Sub

    Private Sub cbotype_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbotype.SelectedIndexChanged
        loadcaaa()
        'cbocategory.Enabled = True
    End Sub
End Class